<?php

namespace App\Console\Commands;

use App\Admin\Models\SiteStore;
use App\Common\Models\StoreRenewalLog;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class GetStoreRenewalLog extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:GetStoreRenewalLog';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '获取到期续费门店列表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info('获取到期续费门店列表开始');
        $this->getstorelist();
        Log::info('获取到期续费门店列表结束');
        return 0;
    }
    protected function getstorelist(){
        // $list=SiteStore::where([['contract_end_at','<',(time()+20*86400)],['fee_log_id',0]])->get()->toArray();
        $list=SiteStore::where([
            ['contract_end_at','<',(time()+30*86400)],
            ['fee_log_id',0]
            ])->get()->toArray();//试用不提醒
        foreach($list as $val){
            $data=[
                'admin_user_id'=>$val['admin_user_id'],
                'cp_id'=>$val['cp_id'],
                'store_id'=>$val['id'],
                'soft_year_fee'=>$val['soft_year_fee'],
                'contract_end_at'=>$val['contract_end_at'] + 365*86400,
                'create_at'=>time(),
                'update_at'=>time(),
            ];
            $log_id=StoreRenewalLog::create($data)->id;
            SiteStore::where(['id'=>$val['id']])
                ->update(['fee_log_id'=>$log_id]);//标记创建的付费订单
        }
        SiteStore::where([
            ['contract_end_at','<',time()],
            ['is_can_use','=',1]
        ])->update(['is_can_use'=>2]);
    }
}
